package com.solr.self.search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import org.apache.lucene.queryParser.QueryParser;


public class KeyAnalysisSupport {
	
	/**
	 * <获取分词关键字并排序>
	 * <功能详细描述>
	 * @param sbpo
	 * @return [参数说明]
	 * 
	 * @return ArrayList<String> [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static ArrayList<String> getAnalyseKeyWord(SolrSearchBasePO sbpo){
		// 搜索关键字
		String keyString = (String) sbpo.getParam("key");
		DictionaryAnalyzer mainsegmenter = null;
		ArrayList<String> keywords = new ArrayList<String>();
		// 分词，加载扩展词典
		if (StringHelper.isNotNullAndEmpty(keyString))
		{
//			if (sbpo.getParam("type").equals(SearchConstants.CORPNAME)) 
//			{
//				 mainsegmenter = new DictionaryAnalyzer(DictionaryManager.getCorpKeyWord());
//			}else {
//				mainsegmenter = new DictionaryAnalyzer(DictionaryManager.getPositions());
//			}
			mainsegmenter = new DictionaryAnalyzer();
			keywords = mainsegmenter.analyse(keyString);
			if (!keywords.contains(keyString) || keywords.size() == 0) 
			{
				keywords.add(keyString);
			}
			Comparator<String> comp = new StrLengthComparator();
			Collections.sort(keywords, comp);
		}
		return keywords;
	}
	
	/**
	 * <拼接基本查询条件>
	 * <功能详细描述>
	 * @param sbpo
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String getQueryContent(SolrSearchBasePO sbpo){
		// 取出其他查询条件
		StringBuffer sb = new StringBuffer();
		Iterator it = sbpo.getParamMap().keySet().iterator();
		String key;
		boolean flag;
		while (it.hasNext()) {
			key = (String) it.next();
				if (key.equals("key") || key.equals("type") || key.equals("fl")) {
				continue;
			}
			flag = false;
			for (String value : (String.valueOf(sbpo.getParamMap().get(key))).split(",")) {
				if (StringHelper.isNullOrEmpty(value)) {
					sb.append(" ( ");
					sb.append(key + " : null " );
					continue;
				}
				if (flag) {
					sb.append(" OR ");
				} else {
					sb.append(" ( ");
				}
				if (key.equals("intDeployTime") ) {
					sb.append(key + " : " + value);
				} else {
					sb.append(key + " : " + QueryParser.escape(value));
				}

				flag = true;
			}
			sb.append(" ) ");
			sb.append(" AND ");
		}
		// 增加权重字段 , 排序
//		sb.append("( *:* OR ( area : 深圳^3 ) ) AND ");
		return sb.toString();
	}
	
	/**
	 * <查询条件分析 分词及封装>
	 * 
	 * @param sbpo
	 */
	public static ArrayList<String> analyseSearchOper(SolrSearchBasePO sbpo) {
		
		// 获取分词关键字并排序
		ArrayList<String> keywords = getAnalyseKeyWord(sbpo);
		// 拼接查询条件
		String content = getQueryContent(sbpo);
		
		// 过滤长词记录
		StringBuffer notsb = new StringBuffer();
		if (keywords.size() > 0)
		{
			for (int i = 0; i < keywords.size(); i++) {
				// 对于【+ - & | ! ( ) { } [ ] ^ ~ * ? : \ 】之类进行转义
				String word = QueryParser.escape(keywords.get(i));
				// 加上搜索关键词 type 表示是职位名搜索还是公司名搜索
				keywords.set(i, content + " ( "
						+ sbpo.getParamMap().get("type") + " : " + word
						+ notsb.toString() + " ) ");
				// 每次将长词作为过滤条件加入notsb
				notsb.append(" - " + sbpo.getParamMap().get("type") + " : " + word);
			}
		}
		else {
			int end = content.lastIndexOf("AND");
			keywords.add(content.substring(0, end));
		}
		return keywords;
	}
	
	/**
	 * <分组查询条件分析分词及封装>
	 * 
	 * @param sbpo
	 */
	public static String analyseFacetSearchOper(SolrSearchBasePO sbpo) {
		// 获取分词关键字并排序
		ArrayList<String> keywords = getAnalyseKeyWord(sbpo);
		// 拼接查询条件
		String content = getQueryContent(sbpo);
		String queryContent = "";
		if (keywords.size() > 0)
		{
			// 过滤长词记录
			StringBuffer notsb = new StringBuffer();
			for (int i = 0; i < keywords.size(); i++) {
				// 对于【+ - & | ! ( ) { } [ ] ^ ~ * ? : \ 】之类进行转义
				String word = QueryParser.escape(keywords.get(i));
				// 加上搜索关键词 type 表示是职位名搜索还是公司名搜索
				keywords.set(i,  "("
						+ sbpo.getParamMap().get("type") + " : " + word
						+ notsb.toString() + ")");
				// 每次将长词作为过滤条件加入notsb
				notsb.append(" - " + sbpo.getParamMap().get("type") + " : " + word);
			}
			StringBuffer sbBuffer = new StringBuffer();
			for (String keyQueryValue : keywords) {
				sbBuffer.append(keyQueryValue + "||");
			}
			queryContent = content + sbBuffer.substring(0, sbBuffer.toString().length()-2);
		}
		else {
			int end = content.lastIndexOf("AND");
			queryContent = (content.substring(0, end));
		}
		return queryContent;
	}

}
